<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="设施id" prop="facilityId">
        <el-input v-model="formData.facilityId" placeholder="请输入设施id" />
      </el-form-item>
      <el-form-item label="设备序列号" prop="deviceSerial">
        <el-input v-model="formData.deviceSerial" placeholder="请输入设备序列号" />
      </el-form-item>
      <el-form-item label="设备名称" prop="ipcSerial">
        <el-input v-model="formData.ipcSerial" placeholder="请输入设备名称" />
      </el-form-item>
      <el-form-item label="通道号" prop="channelNo">
        <el-input v-model="formData.channelNo" placeholder="请输入通道号" />
      </el-form-item>
      <el-form-item label="设备名" prop="deviceName">
        <el-input v-model="formData.deviceName" placeholder="请输入设备名" />
      </el-form-item>
      <el-form-item label="设备上报名称" prop="localName">
        <el-input v-model="formData.localName" placeholder="请输入设备上报名称" />
      </el-form-item>
      <el-form-item label="通道名" prop="channelName">
        <el-input v-model="formData.channelName" placeholder="请输入通道名" />
      </el-form-item>
      <el-form-item label="在线状态：0-不在线，1-在线" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="图片地址（大图），若在萤石客户端设置封面则返回封面图片，未设置则返回默认图片" prop="picUrl">
        <el-input v-model="formData.picUrl" placeholder="请输入图片地址（大图），若在萤石客户端设置封面则返回封面图片，未设置则返回默认图片" />
      </el-form-item>
      <el-form-item label="是否加密：0-不加密，1-加密" prop="isEncrypt">
        <el-input v-model="formData.isEncrypt" placeholder="请输入是否加密：0-不加密，1-加密" />
      </el-form-item>
      <el-form-item label="视频质量：0-流畅，1-均衡，2-高清，3-超清" prop="videoLevel">
        <el-input v-model="formData.videoLevel" placeholder="请输入视频质量：0-流畅，1-均衡，2-高清，3-超清" />
      </el-form-item>
      <el-form-item label="当前通道是否关联IPC：true-是，false-否。设备未上报或者未关联都是false" prop="relatedIpc">
        <el-radio-group v-model="formData.relatedIpc">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否显示，0：隐藏，1：显示" prop="isAdd">
        <el-input v-model="formData.isAdd" placeholder="请输入是否显示，0：隐藏，1：显示" />
      </el-form-item>
      <el-form-item label="camera设备类型" prop="devType">
        <el-select v-model="formData.devType" placeholder="请选择camera设备类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { EzvizDeviceChannelApi, EzvizDeviceChannelVO } from '@/api/yz/ezvizdevicechannel'

/** 萤石设备通道 表单 */
defineOptions({ name: 'EzvizDeviceChannelForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  facilityId: undefined,
  deviceSerial: undefined,
  ipcSerial: undefined,
  channelNo: undefined,
  deviceName: undefined,
  localName: undefined,
  channelName: undefined,
  status: undefined,
  picUrl: undefined,
  isEncrypt: undefined,
  videoLevel: undefined,
  relatedIpc: undefined,
  isAdd: undefined,
  devType: undefined,
})
const formRules = reactive({
  deviceSerial: [{ required: true, message: '设备序列号不能为空', trigger: 'blur' }],
  ipcSerial: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
  channelNo: [{ required: true, message: '通道号不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await EzvizDeviceChannelApi.getEzvizDeviceChannel(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as EzvizDeviceChannelApi.EzvizDeviceChannelVO
    if (formType.value === 'create') {
      await EzvizDeviceChannelApi.createEzvizDeviceChannel(data)
      message.success(t('common.createSuccess'))
    } else {
      await EzvizDeviceChannelApi.updateEzvizDeviceChannel(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    facilityId: undefined,
    deviceSerial: undefined,
    ipcSerial: undefined,
    channelNo: undefined,
    deviceName: undefined,
    localName: undefined,
    channelName: undefined,
    status: undefined,
    picUrl: undefined,
    isEncrypt: undefined,
    videoLevel: undefined,
    relatedIpc: undefined,
    isAdd: undefined,
    devType: undefined,
  }
  formRef.value?.resetFields()
}
</script>